package com.ct.gd.passport.view;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.app.LoaderManager.LoaderCallbacks;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.ct.gd.passport.R;
import com.up91.common.android.view.adapter.SimpleListAdapter;

public abstract class AbsCatalogFragment<T> extends ListFragment implements LoaderCallbacks<List<T>> {
	private int										mLevel;
	private SimpleListAdapter<T>					mListAdapter;
	private LoaderManager							loaderManager;
	private T										mCatalog;
	private List<OnStepIntoNextLevelListener<T>>	mOnStepIntoNextLevelListeners	= new ArrayList<AbsCatalogFragment.OnStepIntoNextLevelListener<T>>();

	public interface OnStepIntoNextLevelListener<T> {
		void onStepIntoNextLevel(T catalog, int level);
	}

	public boolean registerOnStepIntoNextLevelListener(OnStepIntoNextLevelListener<T> listener) {
		return mOnStepIntoNextLevelListeners.add(listener);
	}

	public boolean unregisterOnStepIntoNextLevelListener(OnStepIntoNextLevelListener<T> listener) {
		return mOnStepIntoNextLevelListeners.remove(listener);
	}

	protected final void notifyOnStepIntoNextLevel(T catalog) {
		if (null != mOnStepIntoNextLevelListeners) {
			for (int i = 0; i < mOnStepIntoNextLevelListeners.size(); i++) {
				mOnStepIntoNextLevelListeners.get(i).onStepIntoNextLevel(catalog, mLevel);
			}
		}
	}

	public AbsCatalogFragment() {
		super();
	}

	/**
	 * @param level start from 0 which is the root level
	 */
	public AbsCatalogFragment(int level) {
		this.mLevel = level;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View v = inflater.inflate(R.layout.catalog_list, null);
		return v;
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		mListAdapter = onInitListAdapter();
		setListAdapter(mListAdapter);
		getListView().setItemsCanFocus(false);
	}

	/**
	 * start from 0 which is the root level
	 */
	public int getLevel() {
		return mLevel;
	}

	protected abstract SimpleListAdapter<T> onInitListAdapter();

	public void loadCatalog() {
		load(getCatalogLoader());
	}

	protected Loader<List<T>> getCatalogLoader() {
		loaderManager = getActivity().getSupportLoaderManager();
		Loader<List<T>> loader = loaderManager.getLoader(mLevel);
		if (null == loader) loader = loaderManager.initLoader(mLevel, null, this);
		return loader;
	}

	protected void load(Loader<List<T>> loader) {
		beforeLoad(loader);
		loader.reset();
		loader.startLoading();
	}

	abstract protected void beforeLoad(Loader<List<T>> loader);

	protected T getCatalog() {
		return mCatalog;
	}

	protected final void setCatalog(T catalog) {
		this.mCatalog = catalog;
	}

	@Override
	abstract public Loader<List<T>> onCreateLoader(int id, Bundle args);

	@Override
	public void onLoadFinished(Loader<List<T>> loader, List<T> data) {
		mListAdapter.setData(data);
		mListAdapter.notifyDataSetChanged();
		getListView().clearChoices();
	}

	@Override
	public void onLoaderReset(Loader<List<T>> loader) {
	}

}
